import Vue from 'vue'
import axios from 'axios'
import Router from 'vue-router'
import store from '../store/index'
import api from 'api/api'
import {Message} from 'element-ui'

Vue.use(Router)

var router = new Router({
    routes: [
      {
        path: '/login',
        name: 'login',
        component:(resolve)=>require(['components/login/login'],resolve)
      },
      {
        path: '/main',
        name: 'main',
        component:(resolve)=>require(['components/main/main'],resolve),
        meta: {
          requiresAuth: true, allowBack: false
        }
      },
      {path: '/dashboard', name: 'dashboard',
            component:(resolve)=>require(['components/dashboard/main'],resolve)
      },
      {path: '/map', name: 'map',
        component:(resolve)=>require(['components/map/main'],resolve)
      },
      {
        path: '/',
        // name: 'frame',
        component: (resolve) => require(['components/frame/frame'], resolve),
        children:[
          { path: '', redirect: 'dashboard' },
          { path: 'user',name:'user',component: (resolve)=>require(['components/user/user'],resolve)},
          { path: 'area',name:'area',component: (resolve)=>require(['components/area/area'],resolve)},
          { path: 'device', component: (resolve) => require(['components/device/main'], resolve),
            children: [
              { path: '', redirect: 'boxList' },
              { path: 'boxList',component: (resolve)=>require(['components/device/box-list'],resolve)},
              { path: 'poleList',component: (resolve)=>require(['components/device/pole-list'],resolve)},
              { path: 'sensorList',component: (resolve)=>require(['components/device/sensor/sensor-list'],resolve)},
              { path: 'meterList',component: (resolve)=>require(['components/device/meter/meter-list'],resolve)}
            ]
          },
          { path: 'devMonitor',component: (resolve)=>require(['components/dev-monitor/main'],resolve),
            children: [
              { path: '', redirect: 'rtuList' },
              { path: 'rtuList',component: (resolve)=>require(['components/dev-monitor/rtu-list'],resolve)},
              { path: 'lightList',component: (resolve)=>require(['components/dev-monitor/light-list'],resolve)},
            ]
          },
          {path:'group',component:(resolve)=>require(['components/group/main'],resolve),
            children:[
              { path: '', redirect: 'boxGroup' },
              { path: 'boxGroup',component: (resolve)=>require(['components/group/box-group'],resolve)},
              { path: 'poleGroup',component: (resolve)=>require(['components/group/pole-group'],resolve)},
            ]
          },
          { path: 'work',component: (resolve)=>require(['components/work-manage/main'],resolve),
            children: [
              { path: '', redirect: 'list' },
              { path: 'list',component: (resolve)=>require(['components/work-manage/work-list'],resolve)},
              { path: 'historyWork',component: (resolve)=>require(['components/work-manage/work-history'],resolve)}
            ]
          },
          { path: 'workStatistics',component: (resolve)=>require(['components/work-statistics/main'],resolve)}
        ],
        meta: {
          requiresAuth: true, allowBack: false
        }
      },
      { path: '/', redirect: 'login' },
    ]
})

// 全局路由守卫
router.beforeEach((to, from, next) => {
  store.commit('comm/SET_PATH2', to.path)
  const isLogin = store.state.user.token
  const roleId = store.state.user.userInfo.roleId
  let path=to.path.split('/').splice(0,3)//去掉路由带参数
  let url = path.join('/')

  //临时添加
  let path2=to.path.split('/')
  if(['dashboard','map','lightSystem'].indexOf(path2[1])==-1){
    //综合照明：39.100.245.87:8886（三合）39.100.245.87:8186（鄄城） 39.100.245.87:8087(济南市章丘) 58.18.254.30:8001(内蒙古) 39.100.245.87:8891青岛黄岛
    api.ajaxServe = 'http://39.100.245.87:8886/'//综合照明
    axios.defaults.baseURL = 'http://39.100.245.87:8886/'//综合照明
    // api.ajaxServe =window.location.origin+'/'
    // axios.defaults.baseURL = window.location.origin+'/'//综合照明
  }else{
    //大屏:39.100.245.87:8888(东营),39.100.245.87:8887(鄄城) 39.100.245.87:8889(济南市章丘) 58.18.254.30:8002(内蒙古) 39.100.245.87:8893青岛黄岛
     axios.defaults.baseURL = 'http://39.100.245.87:8888/'
     api.ajaxServe = 'http://39.100.245.87:8888/'
    // axios.defaults.baseURL = window.location.origin+'/'//大屏
    // api.ajaxServe = window.location.origin+'/'
  }
  if (to.matched.some(record => record.meta.requiresAuth) && (!isLogin||isLogin=='')) {
    // Message({showClose: true, message: '登录已超时，请重新登录', type: 'warning'})
    next({path: '/login'})
  } else {
    if(['/login','/main'].indexOf(url)!=-1||
      (roleId==-1&&url=='/role')||
      (roleId==-2&&['/commLog','/operateLog','/commLog','/taskLog','/errorLog'].indexOf(url)!=-1)) return next()
    next()
  }
})
export default router
